package com.team11.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team11.server.mapper.MScoreMapper;
import com.team11.server.mapper.PInformationMapper;
import com.team11.server.mapper.SInformationMapper;
import com.team11.server.mapper.TInformationMapper;
import com.team11.server.pojo.*;
import com.team11.server.pojo.dto.*;
import com.team11.server.service.IMScoreService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author pjc
 * @since 2021-07-08
 */
@Service
public class MScoreServiceImpl extends ServiceImpl<MScoreMapper, MScore> implements IMScoreService {

    @Resource
    private MScoreMapper mScoreMapper;

    @Resource
    private TInformationMapper tInformationMapper;

    @Resource
    private SInformationMapper sInformationMapper;
    @Resource
    private PInformationMapper pInformationMapper;

    /**
     * 获取个人评分列表
     *
     * @param currentPage
     * @param size
     * @param pName
     * @param kcName
     * @return
     */
    @Override
    public RespPageBean getMScoreList(Integer currentPage, Integer size, String pName, String kcName) {
        IPage<MScoreList> page = new Page(currentPage, size);
        IPage<MScoreList> mScoreList = mScoreMapper.getMScoreList(page, pName, kcName);
        RespPageBean respPageBean = new RespPageBean(mScoreList.getTotal(), mScoreList.getRecords());
        return respPageBean;
    }

    /**
     * 根据mId获取个人综合得分详情
     *
     * @param mId
     * @return
     */
    @Override
    public RespPageBean getMScoreDetails(Integer currentPage, Integer size, Integer mId,Integer type) {

        //个人信息详情
        Map map = mScoreMapper.getStudentDetails(mId);
        //获取小组长
        try {
            Integer pId =mScoreMapper.selectOne(new QueryWrapper<MScore>().eq("m_id", mId)).getPId();
            String sid = pInformationMapper.selectOne(new QueryWrapper<PInformation>().eq("p_id", pId)).getPSetLeader();
            String sName=null;
            if(sid!=null){
                sName= sInformationMapper.selectOne(new QueryWrapper<SInformation>().eq("sid", sid)).getSName();
            }
            map.put("leader",sName);
//            list.add(sName);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //获取作业详情
        IPage<MScoreDetails> page = new Page<>(currentPage, size);
        IPage<MScoreDetails> mScoreDetails = mScoreMapper.getMScoreDetails(page, mId,type);
        List list1 = new ArrayList();
        list1.add(mScoreDetails.getRecords());
        list1.add(map);

        RespPageBean respPageBean = new RespPageBean(mScoreDetails.getTotal(), list1);
        return respPageBean;

    }

    /**
     * 老师进行综合打分
     *
     * @param tJobNumber
     * @param mScore
     * @param mContent
     * @return
     */
    @Override
    public RespBean ComprehensiveScore( Integer mId,String tJobNumber, Integer tId, String mScore, String mContent) {
        String number = tInformationMapper.selectOne(new QueryWrapper<TInformation>().eq("t_id", tId)).getTJobNumber();

        if (!tJobNumber.equals(number)) {
            return RespBean.error("权限不足，您不能对该同学进行综合评分!");
        }

        MScore mScore1 = new MScore();
        mScore1.setMScore(mScore);
        mScore1.setMContent(mContent);
        mScore1.setMId(mId);
        if(mScoreMapper.updateById(mScore1)==1){
            return RespBean.success("操作成功!");
        }
        return RespBean.error("操作失败！");
    }
}
